home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / timidsrc.zip / motif_p.c < prev    next >
C/C++ Source or Header  |  1996-05-20  |  5KB  |  183 lines

  1. /* 
  2.  
  3.     TiMidity -- Experimental MIDI to WAVE converter
  4.     Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     motif_pipe.c: written by Vincent Pagel (pagel@loria.fr) 10/4/95
  21.    
  22.     pipe communication between motif interface and sound generator
  23.  
  24.     */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <stdarg.h>
  30. #include <sys/ioctl.h>
  31. #ifdef SOLARIS
  32. #include <sys/filio.h>
  33. #endif
  34.  
  35. #include "config.h"
  36. #include "controls.h"
  37. #include "motif.h"
  38.  
  39. int pipeAppli[2],pipeMotif[2]; /* Pipe for communication with MOTIF process   */
  40. int fpip_in, fpip_out;    /* in and out depends in which process we are */
  41. int pid;                   /* Pid for child process */
  42.  
  43. /* DATA VALIDITY CHECK */
  44. #define INT_CODE 214
  45. #define STRING_CODE 216
  46.  
  47. #define DEBUGPIPE
  48.  
  49. /***********************************************************************/
  50. /* PIPE COMUNICATION                                                   */
  51. /***********************************************************************/
  52.  
  53. void pipe_error(char *st)
  54. {
  55.     fprintf(stderr,"CONNECTION PROBLEM WITH MOTIF PROCESS IN %s BECAUSE:%s\n",
  56.         st,
  57.         sys_errlist[errno]);
  58.     exit(1);
  59. }
  60.  
  61.  
  62. /*****************************************
  63.  *              INT                      *
  64.  *****************************************/
  65.  
  66. void pipe_int_write(int c)
  67. {
  68.     int len;
  69.     int code=INT_CODE;
  70.  
  71. #ifdef DEBUGPIPE
  72.     len = write(fpip_out,&code,sizeof(code)); 
  73.     if (len!=sizeof(code))
  74.     pipe_error("PIPE_INT_WRITE");
  75. #endif
  76.  
  77.     len = write(fpip_out,&c,sizeof(c)); 
  78.     if (len!=sizeof(int))
  79.     pipe_error("PIPE_INT_WRITE");
  80. }
  81.  
  82. void pipe_int_read(int *c)
  83. {
  84.     int len;
  85.     
  86. #ifdef DEBUGPIPE
  87.     int code;
  88.  
  89.     len = read(fpip_in,&code,sizeof(code)); 
  90.     if (len!=sizeof(code))
  91.     pipe_error("PIPE_INT_READ");
  92.     if (code!=INT_CODE)    
  93.     fprintf(stderr,"BUG ALERT ON INT PIPE %i\n",code);
  94. #endif
  95.  
  96.     len = read(fpip_in,c, sizeof(int)); 
  97.     if (len!=sizeof(int)) pipe_error("PIPE_INT_READ");
  98. }
  99.  
  100.  
  101.  
  102. /*****************************************
  103.  *              STRINGS                  *
  104.  *****************************************/
  105.  
  106. void pipe_string_write(char *str)
  107. {
  108.    int len, slen;
  109.  
  110. #ifdef DEBUGPIPE
  111.    int code=STRING_CODE;
  112.  
  113.    len = write(fpip_out,&code,sizeof(code)); 
  114.    if (len!=sizeof(code))    pipe_error("PIPE_STRING_WRITE");
  115. #endif
  116.   
  117.    slen=strlen(str);
  118.    len = write(fpip_out,&slen,sizeof(slen)); 
  119.    if (len!=sizeof(slen)) pipe_error("PIPE_STRING_WRITE");
  120.  
  121.    len = write(fpip_out,str,slen); 
  122.    if (len!=slen) pipe_error("PIPE_STRING_WRITE on string part");
  123. }
  124.  
  125. void pipe_string_read(char *str)
  126. {
  127.     int len, slen;
  128.  
  129. #ifdef DEBUGPIPE
  130.     int code;
  131.  
  132.     len = read(fpip_in,&code,sizeof(code)); 
  133.     if (len!=sizeof(code)) pipe_error("PIPE_STRING_READ");
  134.     if (code!=STRING_CODE) fprintf(stderr,"BUG ALERT ON STRING PIPE %i\n",code);
  135. #endif
  136.  
  137.     len = read(fpip_in,&slen,sizeof(slen)); 
  138.     if (len!=sizeof(slen)) pipe_error("PIPE_STRING_READ");
  139.     
  140.     len = read(fpip_in,str,slen); 
  141.     if (len!=slen) pipe_error("PIPE_STRING_READ on string part");
  142.     str[slen]='\0';        /* Append a terminal 0 */
  143. }
  144.  
  145. int pipe_read_ready()
  146. {
  147.     int num;
  148.     
  149.     ioctl(fpip_in,FIONREAD,&num); /* see how many chars in buffer. */
  150.     return num;
  151. }
  152.  
  153. void pipe_open()
  154. {
  155.     int res;
  156.     
  157.     res=pipe(pipeAppli);
  158.     if (res!=0) pipe_error("PIPE_APPLI CREATION");
  159.     
  160.     res=pipe(pipeMotif);
  161.     if (res!=0) pipe_error("PIPE_MOTIF CREATION");
  162.     
  163.     if ((pid=fork())==0)   /*child*/
  164.     {
  165.         close(pipeMotif[1]); 
  166.         close(pipeAppli[0]);
  167.         
  168.         fpip_in=pipeMotif[0];
  169.         fpip_out= pipeAppli[1];
  170.         
  171.         Launch_Motif_Process(fpip_in);
  172.         /* Won't come back from here */
  173.         fprintf(stderr,"WARNING: come back from MOTIF\n");
  174.         exit(0);
  175.     }
  176.     
  177.     close(pipeMotif[0]);
  178.     close(pipeAppli[1]);
  179.     
  180.     fpip_in= pipeAppli[0];
  181.     fpip_out= pipeMotif[1];
  182. }
  183.